﻿using System;
using GeoAPI.Geometries;
using YJKGridTopologyCal.Geometries.Utilities;

namespace YJKGridTopologyCal.Operation.Predicate
{
	internal class EnvelopeIntersectsVisitor : ShortCircuitedGeometryVisitor
	{
		public EnvelopeIntersectsVisitor(Envelope rectEnv)
		{
			this._rectEnv = rectEnv;
		}

		public bool Intersects { get; private set; }

		protected override void Visit(IGeometry element)
		{
			Envelope envelopeInternal = element.EnvelopeInternal;
			if (!this._rectEnv.Intersects(envelopeInternal))
			{
				return;
			}
			if (this._rectEnv.Contains(envelopeInternal))
			{
				this.Intersects = true;
				return;
			}
			if (envelopeInternal.MinX >= this._rectEnv.MinX && envelopeInternal.MaxX <= this._rectEnv.MaxX)
			{
				this.Intersects = true;
				return;
			}
			if (envelopeInternal.MinY >= this._rectEnv.MinY && envelopeInternal.MaxY <= this._rectEnv.MaxY)
			{
				this.Intersects = true;
			}
		}

		protected override bool IsDone()
		{
			return this.Intersects;
		}

		private readonly Envelope _rectEnv;
	}
}
